﻿/**
 * ...
 * @author Артём Григорьев
 */

package loader;

import client.Channel;
import client.Item;
import client.Category;
import api.ServerApi;

class Loader extends ServerApi
{
	private static inline var serverURL : String = "http://localhost/NAA/server/bin/index.php";
	private static inline var authKey : String = "";
	private static inline var appName : String = "";
	
	
	public function new(viewerId : Int) 
	{
		super(serverURL, authKey, viewerId, appName);
	}
	
	public function getChannels(onLoad : Array<Channel> -> Void)
	{
		var params = {
			method: "get_channels"
		};
		
		var onChannelsLoaded = function(xml : Xml) {
			var parser = new ChannelXmlParser(xml);
			onLoad(parser.parse());
		};
		
		this.request("", params, onChannelsLoaded, onError);
	}
	
	public function getCategory(onLoad : Array<Category> -> Void)
	{
		var params = {
			method: "get_category"
		};
		
		var onCategoriesLoaded = function(xml : Xml) {
			var parser = new CategoryXmlParser(xml);
			onLoad(parser.parse());
		};
		
		this.request("", params, onCategoriesLoaded, onError);
	}
	
	public function getItems(onLoad : Array<Item> -> Void, channelId : UInt)
	{
		var params = {
			method: "get_items",
			ch_id: channelId
		};
		
		var onItemsLoaded = function(xml : Xml) {
			var parser = new ItemXmlParser(xml);
			onLoad(parser.parse());
		};
		
		this.request("", params, onItemsLoaded, onError);
	}
	
	public function FindTag(onLoad : Array<Channel> -> Void, catlist : String) {
		var params = {
			method: "tagsearch", tag: catlist
		};
		var onChannelsLoaded = function(xml : Xml) {
			var parser = new ChannelXmlParser(xml);
			onLoad(parser.parse());
		};
		
		this.request("", params, onChannelsLoaded, onError);	
	}	
	
	private function onError(s : String) : Void
	{
		trace(s);
	}
	
}